import numpy as np
import csv
import matplotlib.pyplot as plt

# 'D:/VR_project/ViewPrediction/online/log/90x160/'

myPath = 'D:/VR_project/ViewPrediction/online/log/5x5/'
liveDeepPath = 'D:/VR_project/LiveDeep_All/LiveDeep/normal/demo_5x5/'
attenPath = 'D:/VR_project/youattention/log/online/'
attenOffPath = 'D:/VR_project/youattention/log/offline/'

myList = ['Alien_3.csv', 'Conan1_3.csv', 'Conan2_3.csv', 'Cooking_3.csv',
          'Rhinos_3.csv', 'Skiing_3.csv',  'Surfing_3.csv', 'War_3.csv']
liveDeepList = ['Alien', 'Conan1', 'Conan2', 'Cooking', 'Rhinos', 'Skiing',  'Surfing', 'War']
attenList = ['Alien_3.csv', 'Conan1_3.csv', 'Conan2_3.csv', 'Cooking_3.csv',
          'Rhinos_3.csv', 'Skiing_3.csv',  'Surfing_3.csv', 'War_3.csv']

nameList = []
frameAccList = [[], [], [], []]
tileAccList = [[], [], [], []]
recallList = [[], [], [], []]
precisionList = [[], [], [], []]

for f in myList:
    nameList.append(f.split('_')[0])
    with open(myPath + f, 'r') as csvfile:
        reader = csv.reader(csvfile)
        rows = [row for row in reader]

        frameAccList[0].append(round(float(rows[-4][1]), 4))
        tileAccList[0].append(round(float(rows[-3][1]), 4))
        recallList[0].append(round(float(rows[-2][1]), 4))
        precisionList[0].append(round(float(rows[-1][1]), 4))

for f in liveDeepList:
    with open(liveDeepPath + f + '/metrics2.csv', 'r') as csvfile:
        reader = csv.reader(csvfile)
        rows = [row for row in reader]

        frameAccList[1].append(round(float(rows[-6][1]), 4))
        tileAccList[1].append(round(float(rows[-5][1]), 4))
        recallList[1].append(round(float(rows[-4][1]), 4))
        precisionList[1].append(round(float(rows[-3][1]), 4))

for f in attenList:
    with open(attenPath + f, 'r') as csvfile:
        reader = csv.reader(csvfile)
        rows = [row for row in reader]

        tileAccList[2].append(round(float(rows[-3][1]), 4))
        recallList[2].append(round(float(rows[-2][1]), 4))
        precisionList[2].append(round(float(rows[-1][1]), 4))

for f in attenList:
    with open(attenOffPath + f, 'r') as csvfile:
        reader = csv.reader(csvfile)
        rows = [row for row in reader]

        tileAccList[3].append(round(float(rows[-3][1]), 4))
        recallList[3].append(round(float(rows[-2][1]), 4))
        precisionList[3].append(round(float(rows[-1][1]), 4))

# TileMetrics
x = np.arange(len(nameList))
width = 0.2
plt.rcParams['font.size'] = 20
fig, ax = plt.subplots(figsize=(20, 8))
plt.bar(x, tileAccList[0], width=width, label='ConvLSTM')
plt.bar(x + width, tileAccList[1], width=width, label='LiveDeep')
plt.bar(x + width * 2, tileAccList[2], width=width, label='Attention Online')
plt.bar(x + width * 3, tileAccList[3], width=width, label='Attention Offline')
plt.grid(linestyle='--')
plt.xticks(x + (3 * width) / 2, nameList, fontsize=20)
plt.ylim(0, 1)
plt.ylabel('AverageAccuracy[%]')
ax.yaxis.set_major_locator(plt.MultipleLocator(0.1))
plt.legend(loc='center', ncol=4, bbox_to_anchor=[0.5, 1.05])
plt.show()

x = np.arange(len(nameList))
width = 0.2
plt.rcParams['font.size'] = 20
fig1, ax1 = plt.subplots(figsize=(20, 8))
plt.bar(x, recallList[0], width=width, label='ConvLSTM')
plt.bar(x + width, recallList[1], width=width, label='LiveDeep')
plt.bar(x + width * 2, recallList[2], width=width, label='Attention Online')
plt.bar(x + width * 3, recallList[3], width=width, label='Attention Offline')
plt.grid(linestyle='--')
plt.xticks(x + (3 * width) / 2, nameList, fontsize=20)
plt.ylim(0, 1)
plt.ylabel('AverageRecall[%]')
ax1.yaxis.set_major_locator(plt.MultipleLocator(0.1))
plt.legend(loc='center', ncol=4, bbox_to_anchor=[0.5, 1.05])
plt.show()

x = np.arange(len(nameList))
width = 0.2
plt.rcParams['font.size'] = 20
fig2, ax2 = plt.subplots(figsize=(20, 8))
plt.bar(x, precisionList[0], width=width, label='ConvLSTM')
plt.bar(x + width, precisionList[1], width=width, label='LiveDeep')
plt.bar(x + width * 2, precisionList[2], width=width, label='Attention Online')
plt.bar(x + width * 3, precisionList[3], width=width, label='Attention Offline')
plt.grid(linestyle='--')
plt.xticks(x + (3 * width) / 2, nameList, fontsize=20)
plt.ylim(0, 1)
plt.ylabel('AveragePrecision[%]')
ax2.yaxis.set_major_locator(plt.MultipleLocator(0.1))
plt.legend(loc='center', ncol=4, bbox_to_anchor=[0.5, 1.05])
plt.show()


